package com.soft.javase.day11;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/9
 */
public class UserLogin {
    public static void main(String[] args) {
        String filePath = "emp.txt";
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        if (Login(filePath, username, password)) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }

    }

    private static boolean Login(String filePath, String username, String password) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] str = line.split("#");
                if (str.length == 2) {
                    String name = str[0].trim();
                    String pwd = str[1].trim();

                    if (name.equals(username) && pwd.equals(password)) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("读取文件时出错: " + e.getMessage());
        }
        return false;
    }
}
